$(document).ready(function() {

    PrimeFaces.widget.DataTable.prototype.restoreOrderStatusFromLocalStore = function() {
        
        var _self = this;
        
        var columnId = localStorage.getItem("sort_columnId");
        var sortOrder = localStorage.getItem("sort_oder");                    
        
        if ( columnId != null ) {
            _self.sort(columnId,sortOrder) ;    
            
            var jqId = PrimeFaces.escapeClientId(columnId);
            $(jqId).addClass('ui-state-active');
            
            if ( sortOrder == 'ASCENDING' ) {                
                $(jqId).find('.ui-sortable-column-icon').addClass('ui-icon-triangle-1-n')
            }
            else {
                $(jqId).find('.ui-sortable-column-icon').addClass('ui-icon-triangle-1-s')
            }
        }
    }

    PrimeFaces.widget.DataTable.prototype.restoreFilterStatusFromLocalStore = function() {
        
        var _self = this;
        
        for ( var i = 0; i < localStorage.length; i++ ) { 
            var key = localStorage.key(i) ;
            if ( key.indexOf("filter") != -1 ) {            
                var jqId = PrimeFaces.escapeClientId(key);
                $(jqId).val(localStorage.getItem(key));
            }
        }
        _self.filter() ;
    }

    PrimeFaces.widget.DataTable.prototype.setupSortEvents = function() {
        
        var _self = this;
        
        _self.restoreOrderStatusFromLocalStore() ;

        $(this.jqId + ' th.ui-sortable-column').
        mouseover(function(){
            $(this).toggleClass('ui-state-hover');
        })
        .mouseout(function(){
            $(this).toggleClass('ui-state-hover');
        }
        ).click(function(event) {

            //Stop event if target is a clickable element inside header
            if($(event.target).is(':not(th,span,.ui-dt-c)')) {
                return;
            }

            PrimeFaces.clearSelection();

            var columnId = $(this).attr('id');

            //Reset previous sorted columns
            $(this).siblings().removeClass('ui-state-active').
            find('.ui-sortable-column-icon').removeClass('ui-icon-triangle-1-n ui-icon-triangle-1-s');

            //Update sort state
            $(this).addClass('ui-state-active');
            var sortIcon = $(this).find('.ui-sortable-column-icon');

            if(sortIcon.hasClass('ui-icon-triangle-1-n')) {
                sortIcon.removeClass('ui-icon-triangle-1-n').addClass('ui-icon-triangle-1-s');

                _self.sort(columnId, "DESCENDING");
                localStorage.setItem("sort_columnId", columnId);
                localStorage.setItem("sort_oder", "DESCENDING");
                PrimeFaces.clearSelection();
            }
            else if(sortIcon.hasClass('ui-icon-triangle-1-s')) {
                sortIcon.removeClass('ui-icon-triangle-1-s').addClass('ui-icon-triangle-1-n');

                _self.sort(columnId, "ASCENDING");
                localStorage.setItem("sort_columnId", columnId);
                localStorage.setItem("sort_oder", "ASCENDING");
            } 
            else {
                sortIcon.addClass('ui-icon-triangle-1-n');

                _self.sort(columnId, "ASCENDING");
                localStorage.setItem("sort_columnId", columnId);
                localStorage.setItem("sort_oder", "ASCENDING");                    
            }
        });
    }
        
    /**
     * Binds filter events to filters
     */
    PrimeFaces.widget.DataTable.prototype.setupFiltering = function() {
        
        var _self = this;
        
        _self.restoreFilterStatusFromLocalStore() ;
        
        filterEvent = _self.cfg.filterEvent == 'enter' ? 'keypress' : 'keyup';

        $(this.jqId + ' thead:first th.ui-filter-column .ui-dt-c .ui-column-filter').each(function(index) {
            var filter = $(this);
     
            if(filter.is('input:text')) {
                PrimeFaces.skinInput(filter);

                filter.bind(filterEvent, function(e) {
                    if(_self.cfg.filterEvent == 'keyup'||(_self.cfg.filterEvent == 'enter' && e.which == $.ui.keyCode.ENTER)){
                        _self.filter(e);

                        e.preventDefault();
                        
                        localStorage.setItem(filter.attr("id"),filter.val()) ;                                                
                    } 
                });
            } 
            else {
                filter.change(function(e) {
                    _self.filter(e);                    
                });
            }
        });
    }
    
    PrimeFaces.widget.DataTable.prototype.clearFilters = function clearFilters() {
    
        localStorage.clear() ;    
        $(this.jqId + ' thead th .ui-column-filter').val('');   
        this.filter() ;
    }

});    


